package cn.iocoder.mall.statistics.application.controller.seller;

import cn.iocoder.common.framework.util.DateUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.common.utils.common.api.vo.Result;
import cn.iocoder.common.utils.common.system.util.JwtUtil;
import cn.iocoder.common.utils.common.util.DateUtils;
import cn.iocoder.common.utils.common.util.json.JacksonUtils;
import cn.iocoder.mall.order.api.OrderCredentialService;
import cn.iocoder.mall.order.api.OrderService;
import cn.iocoder.mall.order.api.bo.OrderBO;
import cn.iocoder.mall.order.api.bo.OrderPayAllChartBO;
import cn.iocoder.mall.order.api.constant.OrderStatusEnum;
import cn.iocoder.mall.order.api.dto.OrderCredentialDetailDTO;
import cn.iocoder.mall.order.api.dto.OrderQueryDTO;
import cn.iocoder.mall.order.api.vo.OrdersVO;
import cn.iocoder.mall.shop.api.ShopService;
import cn.iocoder.mall.shop.api.pojo.ShopDTO;
import cn.iocoder.mall.shop.api.pojo.ShopDetailDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/***
 *@author yinxucun
 *@date 2019-12-26 17:21 
 */
@RestController
@RequestMapping("/seller")
@Slf4j
public class SellerInfoController  {

    @Reference(validation = "true", version = "${dubbo.consumer.ShopService.version}")
    private ShopService shopService;
    @Reference(validation = "true",version = "${dubbo.consumer.OrderService.version}")
    private OrderService orderService;

    @Reference(validation = "true",version = "${dubbo.consumer.OrderCredentialService.version}")
    private OrderCredentialService credentialService;



    @GetMapping("/getSellerPersonalFinanceInfo")
    public Result getSellerPersonalFinanceInfo(){
        Map<String,String> result=new HashMap<>();
        result.put("orderNum","0");
        result.put("incomeOfDay","0");
        result.put("totalIncome", "0");

        List<ShopDetailDTO> list=shopService.getShopLisBySellerId(JwtUtil.getUserIdByToken());

        int totalIncome
                = list.stream()
                .mapToInt(ShopDetailDTO::getIncome)
                .sum();

        log.info(JacksonUtils.toJSONString(list));
        result.put("totalIncome", String.valueOf(totalIncome));

        List<String> shopIds=list.stream().map(ShopDetailDTO::getId).collect(Collectors.toList());
        if (shopIds.isEmpty()){
            return Result.ok(result);
        }
        OrderQueryDTO vo=new OrderQueryDTO();
        vo.setShopIds(shopIds);
        vo.setStartClosingTime(DateUtil.getDayBegin());
        vo.setEndClosingTime(DateUtil.getDayEnd());
        log.info(JacksonUtils.toJSONString(vo));
        CommonResult<List<OrderBO>> orderResult=orderService.getOrderBOListByWhereCondition(vo);
        if (orderResult.isSuccess()){
            log.info(JacksonUtils.toJSONString(orderResult.getData()));

            long a= (long) orderResult.getData().size();
            result.put("orderNum",String.valueOf(a));
                log.info(JacksonUtils.toJSONString(orderResult.getData()));
             long b=orderResult.getData().stream().mapToLong(o->{return o.getPresentPrice()!=null?o.getPresentPrice():0;}).sum();
             result.put("incomeOfDay",String.valueOf(b));
        }

        return Result.ok(result);
    }
    @GetMapping("/getClerkPersonalFinanceInfo")
    public Result getClerkPersonalFinanceInfo(){
        Map<String,String> result=new HashMap<>();
        result.put("orderNum","0");
        result.put("incomeOfDay","0");

        CommonResult<List<ShopDTO>> shopResult=shopService.queryClerkBelongShops(JwtUtil.getUserIdByToken());
        List<ShopDTO> list=shopResult.isSuccess()?shopResult.getData(): Collections.emptyList();
        if (list.isEmpty()){
            return Result.ok(result);
        }
        List<Long> orderIds=new ArrayList<>();
        CommonResult<List<OrderCredentialDetailDTO>> crentResult=credentialService.getOrderCredentialListBySellerIdAndDateRange(JwtUtil.getUserIdByToken(),DateUtils.formatDate(new Date()),DateUtils.formatDate(new Date()));
        if (crentResult.isSuccess()){
             orderIds=crentResult.getData().stream().map(OrderCredentialDetailDTO::getOrderId).collect(Collectors.toList());
        }
        if (orderIds.isEmpty()){
            return Result.ok(result);
        }

        List<String> shopIds=list.stream().map(ShopDTO::getId).collect(Collectors.toList());
        OrderQueryDTO vo=new OrderQueryDTO();
        vo.setShopIds(shopIds);
        vo.setStartClosingTime(DateUtil.getDayBegin());
        vo.setEndClosingTime(DateUtil.getDayEnd());
        log.info(JacksonUtils.toJSONString(vo));
        CommonResult<List<OrderBO>> orderResult=orderService.getOrderBOListByWhereCondition(vo);
        if (orderResult.isSuccess()){
            List<Long> finalOrderIds = orderIds;
            log.info(JacksonUtils.toJSONString(finalOrderIds));
            long a=orderResult.getData().stream().filter(orderPayAllChartBO -> {return orderPayAllChartBO.getId()!=null&& finalOrderIds.stream().anyMatch(c->orderPayAllChartBO.getId().equals(c));}).count();
            result.put("orderNum",String.valueOf(a));

            long b=orderResult.getData().stream().filter(orderPayAllChartBO -> {return orderPayAllChartBO.getId()!=null&& finalOrderIds.stream().anyMatch(c->orderPayAllChartBO.getId().equals(c));}).
                    mapToInt(OrderBO::getPayAmount).sum();
            result.put("incomeOfDay",String.valueOf(b));
        }

        return Result.ok(result);
    }

}
